<template>
  <div class="_wz_batchPartFebruaryAdd">
      <Form ref="formAdd" :model="formData" label-position="right" :label-width="100" :rules="ruleValidate" class="_wz_hiddenTroubleAdd_form">
        <FormItem label="隐患部位" prop="riskPositionUuids">
<!--          public.selectName($event,formData,'riskPositionName')-->
          <i-select v-model="formData.riskPositionUuids" multiple filterable placeholder="请选择隐患部位" clearable>
            <Option v-for="item in riskPositionList" :value="item.uuid" :key="item.uuid">{{ item.riskPositionName }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="排查渠道" prop="riskPlanItemType">
          <i-select v-model.number="formData.riskPlanItemType" filterable placeholder="请选择排查渠道" clearable @on-change="$set(formData, 'riskPlanItemCheckUuids', [])">
            <Option :value="1">管理型单位</Option>
            <Option :value="2">生产型单位</Option>
          </i-select>
        </FormItem>
        <Divider orientation="left">{{formData.riskPlanItemType == 1 ? '管理型单位':'生产型单位'}}</Divider>
        <Transfer :data="userDataList" :titles="['管理型单位','所选人员']" :target-keys="selectUserDataList" filterable :filter-method="filterMethod" @on-change="selectUserDataList = $event" :list-style=" {width: 'calc((100% - 65px) / 2)',height: '400px'}" v-show="formData.riskPlanItemType == 1"></Transfer>
        <Transfer :data="userDataList1" :titles="['生产型单位','所选人员']"  :target-keys="selectUserDataList1" filterable :filter-method="filterMethod" @on-change="selectUserDataList1 = $event" :list-style=" {width: 'calc((100% - 65px) / 2)',height: '400px'}" v-show="formData.riskPlanItemType == 2"></Transfer>
      <div class="demo-drawer-footer" style="z-index: 99;">
        <Button style="margin-right: 8px" @click="$emit('batchChildCloseFun', false)">取消</Button>
        <Button type="primary" @click="addSubmit('formAdd')">提交</Button>
      </div>
    </Form>
    <Spin size="large" fix v-if="spinShow"></Spin>

  </div>
</template>
<script>

export default {
  props: ['parentFormData'],//接收来自父组件的数据
  data() {
    return {
      spinShow:false,//本页加载
      userDataList:[],
      selectUserDataList:[],
      userDataList1:[],
      selectUserDataList1:[],
      ruleValidate: {//正则
        riskPositionUuids: [
          {type:'array',required: true, message: '请选择隐患部位', trigger: 'blur'}
        ],
        riskPlanItemType: [
          {type:'number',required: true, message: '请选择排查渠道', trigger: 'blur'}
        ],
        riskPlanItemCheckUuids: [
          {type:'array',required: true, message: '请选择排查人员', trigger: 'blur'}
        ],
      },
      formData:{},
      riskPositionList:[],//隐患清单
      systemUserList:[],//人员列表
      coreVehicleList:[],//车辆列表
    }
  },
  beforeCreate() {//beforeCreate创建前状态

  },
  created() {//created创建完毕状态

  },
  beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

  },
  mounted() {//mounted 挂载结束状态
    var that = this;

    that.$set(that.formData, 'riskPlanItemType', !!that.formData.riskPlanItemType?that.formData.riskPlanItemType:1)//赋值
    that.screeningCascade(that.parentFormData.deptUuid)

  },
  methods: {//执行的方法
    //穿梭框 使用
    filterMethod (data, query) {
      return data.label.indexOf(query) > -1;
    },
    //排查周期 级联
    screeningCascade(data){
      let that = this;

      //清空table
      that.riskPositionList = [];
      that.systemUserList = [];//人员列表
      that.coreVehicleList = [];//车辆列表
      if(!!data){
        //隐患清单
        that.axios.post(that.apiUrl.riskPositionRiskPositionList, {deptUuid:that.parentFormData.deptUuid}).then(res => {
          if (!!res) {
            that.riskPositionList = res.data.data;
          }
        }).catch(err => {
          console.log("失败", err)
        })

        //人员列表
        that.axios.post(that.apiUrl.systemUserSystemUserList, {deptUuid:that.parentFormData.deptUuid,inOrgType:1}).then(res => {
          if (!!res) {
            res.data.data.forEach(function (k, i) {
              k.key = k.uuid;
              // + k.deptName + ' - '
              k.label = k.name + ' - ' + k.inOrgName + (!!k.inOrgState ?' - 主管' : '');
            })
            that.selectUserDataList = !!that.formData.riskPlanItemCheckUuid ? that.formData.riskPlanItemCheckUuid.split(','):[];
            that.userDataList = res.data.data;
          }
        }).catch(err => {
          console.log("失败", err)
        })

        //驾培人员
        that.axios.post(that.apiUrl.coreDriverCoreDriverList, {deptUuid:that.parentFormData.deptUuid,inOrgType:2}).then(res => {
          if (!!res) {
            res.data.data.forEach(function (k, i) {
              k.key = k.uuid;
              k.label = k.name + ' - ' + k.typeName + ' - ' + k.vehiclePlate + ' - ' + k.inOrgName;
            })
            that.selectUserDataList1 = !!that.formData.riskPlanItemCheckUuid ? that.formData.riskPlanItemCheckUuid.split(','):[];
            that.userDataList1 = res.data.data;
          }
        }).catch(err => {
          console.log("失败", err)
        })
      }
    },
    //提交隐患计划
    addSubmit(data) {
      let that = this;

      that.$refs[data].validate((valid) => {
        if (valid) {//正则判断使用
          if(that.formData.riskPlanItemType == 1){//人员排查
            if(that.selectUserDataList.length < 1){
              that.$Message.error('请选择排查人员');
              return ;
            }


            that.formData.riskPlanItemCheckUuid =  that.selectUserDataList.join(',')
            //人员中文
            that.formData.riskPlanItemCheckName = that.public.extractName(that.userDataList, that.selectUserDataList, {
              uniqueValue: 'uuid',
              name: 'name',
            }).name.join(',')//从业身份中文


          }else{//驾培人员
            if(that.selectUserDataList1.length < 1){
              that.$Message.error('请选择排查人员');
              return ;
            }

            that.formData.riskPlanItemCheckUuid =  that.selectUserDataList1.join(',')
            //人员中文
            that.formData.riskPlanItemCheckName = that.public.extractName(that.userDataList1, that.selectUserDataList1, {
              uniqueValue: 'uuid',
              name: 'name',
            }).name.join(',')//从业身份中文
          }


          // //排查部位
          // that.riskPositionList.forEach(function (k, i) {
          //   if(k.uuid == that.formData.riskPositionUuids){
          //     that.formData.riskPositionType = k.riskPositionType;
          //     return false;
          //   }
          // })

          //排查部位
          var riskPositionUuids = [];
          that.riskPositionList.forEach(function (k, i) {
            that.formData.riskPositionUuids.forEach(function (ko, io) {
              if(k.uuid == ko){
                riskPositionUuids.push({riskPositionName:k.riskPositionName,riskPositionType:k.riskPositionType,riskPositionUuid:k.uuid})
                that.formData.riskPositionType = k.riskPositionType;
                return false;
              }
            })
          })
          that.formData.riskPositionUuidList = riskPositionUuids;
          that.$emit('batchChildCloseFun', true,that.formData)
        } else {
          that.$Message.error('请正确填写表单');
        }
      })
    },

  },
  watch: {//监听

  },
  beforeUpdate() {//beforeUpdate 更新前状态

  },
  updated() {//updated 更新完成状态

  },
  beforeDestroy() {//beforeDestroy 销毁前状态

  },
  destroyed() {//destroyed 销毁完成状态

  }
}
</script>
<style lang="less">
._wz_batchPartFebruaryAdd {
}
</style>
